import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:rxdart/rxdart.dart';
import 'package:schoollifedemo/base/common_function.dart';
import 'package:schoollifedemo/base/dio_network_util.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class PersonalInfoEditPageViewModel extends BaseViewModel {
  PersonalInfoResponse? _personalInfo;
  AllSchoolResponse? _allSchoolResponse;

  AllSchoolResponse? getAllSchoolResponse() => _allSchoolResponse;
  PublishSubject loadingState = PublishSubject<CommonLoadingState?>();

  PersonalInfoResponse? getPersonalInfoResponse() => _personalInfo;

  String getUserSex() {
    String sex = '';
    switch (_personalInfo?.sex) {
      case 0:
        sex = '女';
        break;
      case 1:
        sex = '男';
        break;
      default:
        sex = '保密';
        break;
    }
    return sex;
  }

  void setNewSex(int sex) {
    _personalInfo?.sex = sex;
  }

  void setNickname(String newNickname) {
    _personalInfo?.nickname = newNickname;
  }

  void setNewPhone(String phone) {
    _personalInfo?.phone = phone;
  }

  void setNewStudentId(String studentId) {
    _personalInfo?.studentId = studentId;
  }

  void setNewTeachClass(String newTeachClass) {
    _personalInfo?.teachClass = newTeachClass;
  }

  void setNewSchool(SchoolEntity schoolEntity) {
    _personalInfo?.school = schoolEntity;
  }

  void setNewArea(String area) {
    _personalInfo?.area = area;
  }

  void setPersonalIntroduce(String newInfo) {
    _personalInfo?.personalProfile = newInfo;
  }

  String getIconAddress() {
    return UrlContract.baseAddress + '/' + (_personalInfo?.icon?.iconSrc ?? '');
  }

  // 返回上一层页面
  Future<void> backPage(BuildContext context) async {
    getApplication().routerPop(context);
  }

  // 跳转到头像更改页面
  Future<void> turnToSelectIconPage(BuildContext context) async {
    await getApplication().routerNavigateTo(
      context,
      RoutersPath.selectPersonalIconPage,
      transition: TransitionType.inFromRight,
    );
  }

  // 进行信息修改
  Future<String?> sureToChangePersonalInfo() async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    params['personal_profile'] = _personalInfo?.personalProfile ?? '';
    params['school_id'] = _personalInfo?.school?.schoolId ?? '';
    params['sex'] = _personalInfo?.sex ?? 3;
    params['area'] = _personalInfo?.area ?? '';
    params['phone'] = _personalInfo?.phone ?? '';
    params['nickname'] = _personalInfo?.nickname ?? '';
    params['student_id'] = _personalInfo?.studentId ?? '';
    params['icon_id'] = _personalInfo?.icon?.iconId ?? '';
    params['teacher_id'] = _personalInfo?.teacherId ?? '';
    params['teach_class'] = _personalInfo?.teachClass ?? '';
    final result = await getNetworkUtil().postHttp(
      UrlContract.modifyPersonalInfo,
      params: params,
    );
    if (result == null) {
      return 'server result error';
    }
    final resp = CommonResponse.fromMap(result);
    if (resp.status == 0) {
      return null;
    } else {
      return 'server error msg :${resp.msg}';
    }
  }

  // 拉取个人信息
  Future<void> pullPersonalInfo({
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    final params = <String, Object>{};
    params['account'] = getApplication().getLoginAccount() ?? '';
    Map<String, dynamic>? json = await getNetworkUtil().postHttp(
      UrlContract.pullPersonalInfo,
      params: params,
    );
    _personalInfo = PersonalInfoResponse.fromMap(json);
    if (_personalInfo?.status == 0) {
      loadingState.add(CommonLoadingState.successTypeOne);
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      final commonResp = CommonResponse.fromMap(json);
      loadingState.add(CommonLoadingState.failure);
      if (onFailure != null) {
        onFailure(commonResp.msg);
      }
    }
  }

  // 拉取全部学校信息
  Future<void> pullAllSchoolInfo({
    OnSuccess? onSuccess,
    OnFailure? onFailure,
  }) async {
    Map<String, dynamic>? json = await getNetworkUtil().postHttp(
      UrlContract.pullAllSchoolList,
    );
    final resp = AllSchoolResponse.fromMap(json);
    if (resp.status == 0) {
      _allSchoolResponse = resp;
      if (onSuccess != null) {
        onSuccess();
      }
    } else {
      if (onFailure != null) {
        onFailure(resp.msg);
      }
    }
  }
}
